package com.icss.ah.productForSale.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.productForSale.service.ProductForSaleService;

/**
 * 店铺申请Action
 * @author sw
 *
 */
@Controller
@Scope("prototype")
public class ProductForSaleAction extends BaseAction {
	
	@Autowired
	private ProductForSaleService productForSaleService;
	
	public String toProductForSalePage(){
		String returnPage = "toProductForSalePage";

		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		
		request = this.getRequest();
		HashMap map  = new HashMap();
		map.put("shopId", shopId);
		map.put("corpId", corpId);
		List yjfl =  productForSaleService.getProductType1List("ProductForSaleBean.getProductType1List", map);
		
		request.setAttribute("yjfl", yjfl);
		return returnPage;
	}
	
	public void getProductType2List(){
		request = this.getRequest();
		
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();

		String firstSeries  = request.getParameter("firstSeries")==null?"":request.getParameter("firstSeries");
		
		String result = "";
		try {
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",corpId);	//公众号id
			map.put("shopId", shopId);	//商铺id
			map.put("firstSeries", firstSeries);
		
			List<Map> ejfl =  productForSaleService.getProductType2List("ProductForSaleBean.getProductType2List", map);
			map.put("ejfl", ejfl);
			System.out.println(">>>>>>>>>>>>>>>"+ejfl);
			result = JSONObject.fromObject(map).toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			str2Resp(result);
		}
	}
	
	public void productForSaleList(){
		request = this.getRequest();
		
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		
		String order  = request.getParameter("order");
		if(order==null||"".equals(order)){
			order = " UP_DATE DESC ";
		}
		String title  = request.getParameter("title").trim();
		if(title==null||"".equals(title)){
			title = "";
		}
		String minPriceStr  = request.getParameter("minPriceStr")==null?"":request.getParameter("minPriceStr").trim();
		String maxPriceStr  = request.getParameter("maxPriceStr")==null?"":request.getParameter("maxPriceStr").trim();
		
		String firstSeries  = request.getParameter("firstSeries")==null?"0":request.getParameter("firstSeries").trim();
		String secondSeries  = request.getParameter("secondSeries")==null?"0":request.getParameter("secondSeries").trim();
		String serise ="";
		if(secondSeries==null||"".equals(secondSeries)||"0".equals(secondSeries)||"null".equals(secondSeries)){
			if(firstSeries!=null&&!"null".equals(firstSeries)&&!"0".equals(firstSeries)) 
			serise = firstSeries==null?"":firstSeries;
		}else{
			 serise = secondSeries;
		}
		
		String result = "";
		try {
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",corpId);	//公众号id
			map.put("shopId", shopId);	//商铺id
			map.put("order", order);
			map.put("title", title);
			map.put("minPriceStr", minPriceStr);
			map.put("maxPriceStr", maxPriceStr);
			map.put("serise",serise);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			
			List<Map> productList = new ArrayList();
			productList = productForSaleService.getProductList("ProductForSaleBean.getProductList", map);
			long productCount = productForSaleService.getProductCount("ProductForSaleBean.getProductCount", map);//查询总记录数
			
			
			
			dataGridJson.setRows(productList);
			dataGridJson.setTotal(productCount);
			result = JSONObject.fromObject(dataGridJson).toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			str2Resp(result);
		}
	}
	
	public void productDown(){
		jsonText = new Json();
		request = this.getRequest();
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		String productIds = request.getParameter("productIds");
		String[] ids = productIds.split(",");
		for(int i=0;i<ids.length;i++){
			if(!"".equals(ids[i])){
				HashMap map  = new HashMap();
				map.put("productId", ids[i]);
				map.put("corpId", corpId);
				map.put("shopId", shopId);
				productForSaleService.productDown("ProductForSaleBean.productDown", map);
			}
			
		}
		jsonText.setSuccess(true);
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	
	public void productDel(){
		jsonText = new Json();
		request = this.getRequest();
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		String productIds = request.getParameter("productIds");
		String[] ids = productIds.split(",");
		for(int i=0;i<ids.length;i++){
			if(!"".equals(ids[i])){
				HashMap map  = new HashMap();
				map.put("productId", ids[i]);
				map.put("corpId", corpId);
				map.put("shopId", shopId);
				productForSaleService.productDown("ProductForSaleBean.productDel", map);
			}
			
		}
		jsonText.setSuccess(true);
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	public void addStockSave(){
		jsonText = new Json();
		request = this.getRequest();
		try{
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();
			String productId = request.getParameter("productId");
			String addAmount = request.getParameter("addAmount");
			String addPrice = request.getParameter("addPrice");
			HashMap hm = new HashMap();
			hm.put("productId", productId);
			hm.put("addAmount", addAmount);
			hm.put("addPrice", addPrice);
			hm.put("shopId", shopId);
			boolean result = productForSaleService.addStockSave(hm);
			jsonText.setSuccess(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	
	public void alarmSettingSave(){
		jsonText = new Json();
		request = this.getRequest();
		try{
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();
			String productId = request.getParameter("productId");
			String stockWarning = request.getParameter("stockWarning");
			Map<String, String> map = new HashMap<String, String>();
			map.put("shopId", shopId);
			map.put("productId", productId);
			map.put("stockWarning", stockWarning);
			boolean result = productForSaleService.alarmSettingSave(map);
			jsonText.setSuccess(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	public Json getJsonText() {
		return jsonText;
	}

	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}
	
}
